package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import com.alibaba.fastjson.JSONObject
import com.wisdge.dataservice.Result
import com.wisdge.dataservice.xhr.XHRPoolService
import com.wisdge.web.springframework.SpringContextUtil
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import cn.com.kh.KHUtil
import cn.com.kh.getProperty

public class getShortenLink implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(getShortenLink.class);

    private static final String DOMAIN = getProperty.get("NGS.Domain");

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");

        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);

        if (getMethod.equals("GET")) {

            String dynid = request.getParameter("dynid");
            String userid = request.getParameter("userid");
            String usertype = request.getParameter("usertype");
            String recid = request.getParameter("recid");
            String expand = request.getParameter("expand");
            String cbtype = request.getParameter("cbtype");
            log.debug("原始Get请求参数-dynid:" + dynid + " userid:" + userid + " usertype:" + usertype + " recid:" + recid + " expand:" + expand + " cbtype:" + cbtype);

            String checkNullInfo = KHUtil.isNullMSG("dynid", dynid) + KHUtil.isNullMSG("userid", userid);
            log.debug("非空校验检查结果:" + checkNullInfo);
            if (!KHUtil.isNull(checkNullInfo)) {
                if (cbtype.equals("redirect")) {
                    response.sendRedirect(DOMAIN + "/ngs/htmls/500/index.jsp");
                } else {
                    return JSONObject.toJSONString(new Result(Result.ERROR, checkNullInfo));
                }

            } else {

                try {
                    String url = DOMAIN + "/ngs/S";

                    // 获取短码标准入参
                    JSONObject content = new JSONObject();
                    content.put("at", "dyn2");
                    content.put("mt", "r");
                    content.put("ai", dynid);

                    // 客制化参数部分
                    JSONObject requestParams = new JSONObject();
                    requestParams.put("recid", recid);
                    requestParams.put("expand", expand);
                    requestParams.put("userid", userid);
                    requestParams.put("usertype", usertype);
                    content.put("params", requestParams);

                    // 封装调用参数
                    JSONObject params = new JSONObject();
                    params.put("ttl", 0);
                    params.put("content", content.toJSONString());
                    log.debug("封装调用参数params:" + params.toString());

                    // 发起调用
                    Map<String, String> headMap = new HashMap<String, String>();
                    headMap.put("Content-Type", "application/json");

                    String resultBody = Util.doHttpPostUseHead(url, params.toString(),"UTF-8",5000,0,headMap);
                    log.debug("调用返回resultBody:" + resultBody);

                    // 解析调用参数
                    JSONObject resultJson = JSONObject.parseObject(resultBody);
                    if (resultJson.getInteger("code") == 1) {
                        log.debug("调用NGS生成短码成功！" + resultJson.getString("value"));

                        if (cbtype.equals("redirect")) {
                            response.sendRedirect(DOMAIN + "/ngs/m/nl?" + resultJson.getString("value"));
                        } else {
                            return JSONObject.toJSONString(new Result(Result.SUCCESS, DOMAIN + "/ngs/m/nl?" + resultJson.getString("value")));
                        }
                    } else {

                        log.warn("调用NGS生成短码错误！", resultJson.getString("message"));

                        if (cbtype.equals("redirect")) {
                            response.sendRedirect(DOMAIN + "/ngs/htmls/500/index.jsp");
                        } else {
                            return JSONObject.toJSONString(new Result(Result.WARN, "调用NGS生成短码错误！", resultJson.getString("message")));
                        }
                    }

                } catch (Exception e) {
                    log.error("调用NGS生成短码链接异常：", e);

                    if (cbtype.equals("redirect")) {
                        response.sendRedirect(DOMAIN + "/ngs/htmls/500/index.jsp");
                    } else {
                        return JSONObject.toJSONString(new Result(Result.ERROR, "调用NGS生成短码链接异常：" + e.getMessage()));
                    }
                }
            }

        } else {

            log.error("HTTP请求调用类型错误，接口调用失败：" + getMethod);
            return JSONObject.toJSONString(new Result(Result.ERROR, "HTTP请求调用类型错误，接口调用失败：" + getMethod));
        }
    }
}
